package com.leave.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leave.entity.Role;
import com.leave.service.IRoleService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class RoleController {

	@Autowired
	private IRoleService roleService;
	
	@RequestMapping("/getAllRoleByPage")
	@ResponseBody
	public String getAllRoleByPage(Integer page,Integer rows) {
		HashMap<String,Object> map = roleService.getAllRoleByPage(page, rows);
		List<Role> roleList = (List<Role>) map.get("roleList");
		JSONArray jsArr = JSONArray.fromObject(roleList);
		JSONObject js = new JSONObject();
		js.put("total", map.get("total"));
		js.put("rows", jsArr);
		return js.toString();
	}
	
	@RequestMapping(value="/getRoleByRoleId",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getRoleByRoleId(Integer roleId) {
		Role role = roleService.getRoleByroleId(roleId);
		JSONObject js = new JSONObject();
		js.put("role", role);
		return js.toString();
	}
	
	@RequestMapping("/removeRoleByArrId")
	@ResponseBody
	public String removeRoleByArrId(int[] arrId) {
		int i = roleService.removeRoleByArrId(arrId);
		if(i == arrId.length) {
			return "success";
		}
		return "false";
	}
	
	@RequestMapping("/addRole")
	@ResponseBody
	public String addRole(Role r) {
		Role role = roleService.getRoleByRoleName(r.getRoleName());
		if(role !=null) {
			return "exist";
		}
		int i = roleService.addRole(r);
		if(i > 0) {
			return "success";
		}
		return "false";
	}
	
	@RequestMapping("/updateRoleByRole")
	@ResponseBody
	public String updateRoleByRole(Role r) {
		Role role = roleService.getRoleByRoleName(r.getRoleName());
		if(role !=null && !role.getRoleName().equals(r.getRoleName())) {
			return "exist";
		}
		int i = roleService.updateRoleByRole(r);
		if(i > 0) {
			return "success";
		}
		return "false";
	}
	
	
	
	
	
	
	
	
	
	
	
	
}
